package co.fitstart.admin.web.controller.content;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import co.fitstart.admin.service.DataService;
import co.fitstart.admin.web.interceptor.MenuSetting;
import co.fitstart.entity.content.page.Brief;
import co.fitstart.entity.content.page.Link;
import co.fitstart.entity.content.page.Webpage;

import com.demo2do.core.support.Result;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.resolver.Preloading;

/**
 * 
 * @author lidan
 *
 */
@Controller
@RequestMapping("/webpages")
@MenuSetting("menu-content")
public class WebpageController {

    private static final Logger logger = LoggerFactory.getLogger(WebpageController.class);
    
    @Autowired
    @Qualifier("dataService")
    private DataService<Webpage> webpageService;
    
    @Autowired
    @Qualifier("dataService")
    private DataService<Link> linkService;
    
    @Autowired
    @Qualifier("dataService")
    private DataService<Brief> briefService;
    
    /**
     * List all webpages
     * 
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @MenuSetting("submenu-webpage-management")
    public ModelAndView list(){
        
        ModelAndView modelAndView = new ModelAndView("content/webpage/webpage-list");
        
        List<Webpage> webpages = webpageService.listAll(Webpage.class);
        
        modelAndView.addObject("webpages", webpages);
        
        return modelAndView;
    }
    
    /**
     * Display webpage view page
     * 
     * @param webpageId
     * @return
     */
    @RequestMapping(value = "/{webpageId}", method = RequestMethod.GET)
    @MenuSetting("submenu-webpage-management")
    public ModelAndView view(@PathVariable("webpageId") Long webpageId){
        
        ModelAndView modelAndView = new ModelAndView("content/webpage/webpage-view");
        
        Webpage webpage = webpageService.load(Webpage.class, webpageId);
        
        modelAndView.addObject("webpage", webpage);
        
        return modelAndView;
    }
    
    /**
     * Display link list
     * 
     * @param webpageId
     * @return
     */
    @RequestMapping(value = "/{webpageId}/links", method = RequestMethod.GET)
    @MenuSetting("submenu-webpage-management")
    public ModelAndView links(@PathVariable("webpageId") Long webpageId){
        
        ModelAndView modelAndView = new ModelAndView("content/webpage/webpage-link-list");
        
        Webpage webpage = webpageService.load(Webpage.class, webpageId);
        
        modelAndView.addObject("links", webpage.getLinks());
        
        modelAndView.addObject("webpageId", webpageId);
        
        return modelAndView;
    }
    
    /**
     * Display brief list
     * 
     * @param webpageId
     * @return
     */
    @RequestMapping(value = "/{webpageId}/briefs", method = RequestMethod.GET)
    @MenuSetting("submenu-webpage-management")
    public ModelAndView briefs(@PathVariable("webpageId") Long webpageId){
        
        ModelAndView modelAndView = new ModelAndView("content/webpage/webpage-brief-list");
        
        Webpage webpage = webpageService.load(Webpage.class, webpageId);
        
        modelAndView.addObject("briefs", webpage.getBriefs());
        
        modelAndView.addObject("webpageId", webpageId);
        
        return modelAndView;
    }
    
    /**
     * Display edit link panel
     * 
     * @param linkId
     * @param webpageId
     * @return
     */
    @RequestMapping(value = "/{webpageId}/links/{linkId}/edit", method = RequestMethod.GET)
    public ModelAndView editLink(@PathVariable("linkId") Long linkId, @PathVariable("webpageId") Long webpageId){
        
        ModelAndView modelAndView = new ModelAndView("content/webpage/webpage-link-view");
        
        Link link = linkService.load(Link.class, linkId);
        
        modelAndView.addObject("link", link);
        
        modelAndView.addObject("webpageId", webpageId);
        
        return modelAndView;
    }
    
    /**
     * Do edit link
     * 
     * @param link
     * @return
     */
    @RequestMapping(value="/{webpageId}/links/{linkId}", method=RequestMethod.PATCH)
    public @ResponseBody String onEditLink(@Preloading("linkId") Link link){
        
        Result result = new Result();
        
        try{
        
            linkService.update(link);
            
            result.success().message("修改链接成功");
            
        }catch(Exception e){
            
            logger.error("WebpageController#onEditLink() error:", e);
            
            result.fail().message("修改链接失败");
        
        }
        
        return JsonUtils.toJsonString(result);
    }
    
    
    /**
     * Display edit brief panel
     * 
     * @param briefId
     * @param webpageId
     * @return
     */
    @RequestMapping(value = "/{webpageId}/briefs/{briefId}/edit", method = RequestMethod.GET)
    public ModelAndView editBrief(@PathVariable("briefId") Long briefId, @PathVariable("webpageId") Long webpageId){
        
        ModelAndView modelAndView = new ModelAndView("content/webpage/webpage-brief-view");
        
        Brief brief = briefService.load(Brief.class, briefId);
        
        modelAndView.addObject("brief", brief);
        
        modelAndView.addObject("webpageId", webpageId);
        
        return modelAndView;
    }
    
    /**
     * Do edit brief
     * 
     * @param brief
     * @return
     */
    @RequestMapping(value = "/{webpageId}/briefs/{briefId}", method = RequestMethod.PATCH)
    public @ResponseBody String onEditBrief(@Preloading("briefId") Brief brief){
        
        Result result = new Result();
        
        try{                
            
            briefService.update(brief);
            
            result.success().message("修改文案成功");
            
        }catch(Exception e){
            
            logger.error("WebpageController#OnBriefEdit() error:", e);
            
            result.fail().message("修改文案失败");
        }
        
        return JsonUtils.toJsonString(result);
    }

    
}
